package com.pvdnc.systemdebugger.ipcmanager;

import android.annotation.SuppressLint;
import android.app.AppOpsManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.system.Os;
import android.util.Log;

import com.pvdnc.systemdebugger.compacter.utils.AppContext;

import java.lang.reflect.Method;

public class AppOps {
    private static final String TAG="AppOps";

    public static boolean setUidMode() {
        if (AppContext.getAppContext().checkCallingOrSelfPermission("android.permission.MANAGE_APP_OPS_MODES") != PackageManager.PERMISSION_GRANTED)
            return false;
        AppOpsManager appOpsManager = (AppOpsManager) AppContext.getAppContext().getSystemService(Context.APP_OPS_SERVICE);
        //public void setUidMode(int code, int uid, int mode)
        try {
            @SuppressLint("PrivateApi")
            Method setUidMode = AppOpsManager.class.getDeclaredMethod("setUidMode", int.class, int.class, int.class);
            setUidMode.setAccessible(true);
            //public static final int OP_SYSTEM_ALERT_WINDOW = 24;
            setUidMode.invoke(appOpsManager, 24, Os.getuid(), AppOpsManager.MODE_ALLOWED);
            Log.d(TAG,"setting AppOps permission finished");
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
